package com.boomshake.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boomshake.entity.Store;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 门店Mapper接口
 *
 * @author BoomShake
 * @date 2024-12-15
 */
@Mapper
public interface StoreMapper extends BaseMapper<Store> {

    /**
     * 分页查询门店列表
     *
     * @param page 分页对象
     * @param name 门店名称（可选）
     * @param status 门店状态（可选）
     * @return 门店列表
     */
    @Select("<script>" +
            "SELECT * FROM store " +
            "WHERE 1=1 " +
            "<if test='name != null and name != \"\"'>" +
            "AND name LIKE CONCAT('%', #{name}, '%') " +
            "</if>" +
            "<if test='status != null'>" +
            "AND status = #{status} " +
            "</if>" +
            "ORDER BY created_at DESC" +
            "</script>")
    IPage<Store> selectStorePage(Page<Store> page, @Param("name") String name, @Param("status") Integer status);

    /**
     * 根据门店编码查询门店
     *
     * @param code 门店编码
     * @return 门店信息
     */
    @Select("SELECT * FROM store WHERE code = #{code}")
    Store selectByCode(@Param("code") String code);

    /**
     * 获取所有营业中的门店
     *
     * @return 门店列表
     */
    @Select("SELECT * FROM store WHERE status = 1 ORDER BY created_at ASC")
    List<Store> selectActiveStores();

    /**
     * 统计各状态门店数量
     *
     * @return 统计结果
     */
    @Select("SELECT status, COUNT(*) as count FROM store GROUP BY status")
    List<StoreStatusCount> selectStatusStatistics();

    /**
     * 门店状态统计内部类
     */
    class StoreStatusCount {
        private Integer status;
        private Integer count;

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }
    }

    /**
     * 根据ID获取门店名称
     * @param id 门店ID
     * @return 门店名称
     */
    @Select("SELECT name FROM store WHERE id = #{id}")
    String getStoreNameById(@Param("id") Long id);
} 